package com.eric.demo.ringbuffer;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;

/**
 * @author guangyin.zhang
 * @since 2019/3/23
 */
public class LongEventMain2 {


    public static void main(String[] args) throws InterruptedException {
        LongEventFactory factory = new LongEventFactory();

        int bufferSize = 4;

        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, DaemonThreadFactory.INSTANCE);

        disruptor.handleEventsWith(
            (event, sequence, endOfBatch) ->
                System.out.println("Thread1:" + Thread.currentThread().getName() + "Event: " + event + ", sequence: " + sequence + ", endOfBatch: " + endOfBatch)
        );

        disruptor.handleEventsWith(
            (event, sequence, endOfBatch) -> {
                System.out.println("Thread2:" + Thread.currentThread().getName() + "Event: " + event + ", sequence: " + sequence + ", endOfBatch: " + endOfBatch);
                Thread.sleep(10000);
            }
        );

        disruptor.start();

        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        for(long l =0;true;l++){
            byteBuffer.putLong(0, l);
            ringBuffer.publishEvent((event, sequence, bb) -> event.set(bb.getLong(0)), byteBuffer);
            Thread.sleep(100);
        }

    }




}
